package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.R;
import com.hyphenate.exceptions.HyphenateException;

public class EaseChatRowDate extends EaseChatRow {

    private TextView tvSubject, tvTime, tvAddress, tvSee;

    private long orderId;

    public EaseChatRowDate(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
        try {
            orderId = Long.parseLong(message.getStringAttribute(EaseConstant.PARAMS_ORDER_ID));
        } catch (HyphenateException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_date : R.layout.ease_row_sent_date, this);
    }

    @Override
    protected void onFindViewById() {
        tvSubject = (TextView) findViewById(R.id.tv_subject);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvSee = (TextView) findViewById(R.id.tv_see);
    }

    @Override
    protected void setClickListener() {
        tvSee.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (itemClickListener != null){
                    if(!itemClickListener.onBubbleClick(message)){
                        //如果listener返回false不处理这个事件，执行lib默认的处理
                        onBubbleClick();
                    }
                }
            }
        });

        if (statusView != null) {
            statusView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onResendClick(message);
                    }
                }
            });
        }

        userAvatarView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    if (message.direct() == EMMessage.Direct.SEND) {
                        itemClickListener.onUserAvatarClick(EMClient.getInstance().getCurrentUser());
                    } else {
                        itemClickListener.onUserAvatarClick(message.getFrom());
                    }
                }
            }
        });

    }

    @Override
    public void onSetUpView() {
        try {
            String mSubject = message.getStringAttribute(EaseConstant.PARAMS_SUBJECT);
            String mTime = message.getStringAttribute(EaseConstant.PARAMS_TIME);
            String mAddress = message.getStringAttribute(EaseConstant.PARAMS_ADDRESS);

            tvSubject.setText(mSubject);
            tvTime.setText(mTime);
            tvAddress.setText(mAddress);

        } catch (HyphenateException e) {
        }

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
//                sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub

    }


}
